package top.laoshuzi.server.camera.service.impl

import com.alibaba.fastjson.JSON
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import top.laoshuzi.server.camera.bean.dto.LightStateCmdDTO
import top.laoshuzi.server.camera.bean.dto.LightStateDTO
import top.laoshuzi.server.camera.config.CameraConstant
import top.laoshuzi.server.camera.mqtt.MqttConsumer
import top.laoshuzi.server.camera.mqtt.MqttTemplate
import top.laoshuzi.server.camera.service.LightService

@Service
class LightServiceImpl : LightService {


    @Autowired
    private lateinit var mqttTemplate: MqttTemplate

    override fun setLightState(id: String, state: LightStateDTO) {
        val cmd = LightStateCmdDTO().apply {
            led_g = state.green
            led_r = state.red
            led_b = state.blue
        }
        mqttTemplate.sendToMqtt(CameraConstant.TOPIC_LIGHT.replace("{id}", id), JSON.toJSONString(cmd))
    }

    override fun getLightState(id: String): LightStateDTO {
        return MqttConsumer.lightStateMap[id] ?: throw RuntimeException("设备不存在")
    }

}